package duppentity.dto.bu;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import constants.BuDefault;
import utils.JSONObjectUtils;
import utils.JsonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 将dupp返回的数据重新进行封装
 * Author: pengpeng@djcars.cn
 * Date: 2020/1/7 10:20
 */

public class FaultUserDtoUtil {
    public static List<FaultUserDto> getFaultUserDtoList(JsonObject data, boolean isEdit){
        List<FaultUserDto> faultUserDtoList = null;
        if (data!=null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    if (list != null && list.size() > 0) {
                        faultUserDtoList = new ArrayList<>();
                        for (Integer i=0;i<list.size();i++) {
                            JSONObject listOne = JSONObjectUtils.get(list, i);
                            FaultUserDto faultUserDto = convertFaultUserDto(listOne, data, isEdit);
                            faultUserDtoList.add(faultUserDto);
                        }
                    }
                }
            }
        }
        return  faultUserDtoList;
    }

    public static FaultUserDto getFaultUserDto(JsonObject data, boolean isEdit) {
        FaultUserDto faultUserDto =new FaultUserDto();
        if (data!=null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    JSONObject listOne = JSONObjectUtils.get(list, 0);
                    faultUserDto = convertFaultUserDto(listOne,data, isEdit);
                }
            }
        }
        return faultUserDto;
    }

    private static FaultUserDto convertFaultUserDto(JSONObject listOne, JsonObject data, boolean isEdit){
        FaultUserDto faultUserDto = new FaultUserDto();
        faultUserDto.id = listOne.getLongValue("id");
        faultUserDto.mobile = listOne.getString("mobile");
        faultUserDto.name = listOne.getString("name");
        faultUserDto.createTime = listOne.getLongValue("createTime");
        faultUserDto.modifyTime = listOne.getLongValue("modifyTime");
        JSONArray buStationFaultUserIds = listOne.getJSONArray("buStationFaultUserIds");
        if (buStationFaultUserIds != null && buStationFaultUserIds.size() > 0) {
            for (Object stationFaultUserId : buStationFaultUserIds) {
                JSONObject buStationFaultUser = JSONObjectUtils.get(data, "bu_station_fault_user");
                String stationId = JSONObjectUtils.getString(buStationFaultUser, stationFaultUserId.toString(), "stationId");
                JSONObject buStation = JSONObjectUtils.get(data, "bu_station");
                String name = JSONObjectUtils.getString(buStation, stationId, "name");
                faultUserDto.stationNames.add(name);
                if (isEdit) {
                    JsonObject jsonObject = new JsonObject();
                    jsonObject.addProperty("stationFaultUserId", stationFaultUserId.toString());
                    jsonObject.addProperty("name", name);
                    faultUserDto.stationName.add(jsonObject);
                }
            }
        }
        return  faultUserDto;
    }

    public static List<StationFaultRecordDto> getStationFaultRecordDtoList(JsonObject data){
        List<StationFaultRecordDto> stationFaultRecordDtoList = null;
        if (data!=null) {
            if (data.has("total")) {
                if (data.get("total").getAsInt() > 0) {
                    JsonArray list = data.getAsJsonArray("list");
                    if (list != null && list.size() > 0) {
                        stationFaultRecordDtoList = new ArrayList<>();
                        for (Integer i=0;i<list.size();i++) {
                            JSONObject listOne = JSONObjectUtils.get(list, i);
                            StationFaultRecordDto stationFaultRecordDto = convertStationFaultRecordDto(listOne, data);
                            stationFaultRecordDtoList.add(stationFaultRecordDto);
                        }
                    }
                }
            }
        }
        return  stationFaultRecordDtoList;
    }
    private static StationFaultRecordDto convertStationFaultRecordDto(JSONObject listOne, JsonObject data){
        StationFaultRecordDto stationFaultRecordDto = new StationFaultRecordDto();
        stationFaultRecordDto.id = listOne.getString("id");
        stationFaultRecordDto.stationId = listOne.getLongValue("stationId");
        stationFaultRecordDto.deviceType = listOne.getIntValue("deviceType");
        stationFaultRecordDto.faultDeviceId = listOne.getLongValue("faultDeviceId");
        stationFaultRecordDto.deviceName = BuDefault.getResultName(stationFaultRecordDto.deviceType, BuDefault.nameType.deviceType);
        stationFaultRecordDto.createTime = listOne.getLongValue("createTime");
        stationFaultRecordDto.restoreTime = listOne.getLongValue("restoreTime");
        stationFaultRecordDto.modifyTime = listOne.getLongValue("modifyTime");
        return  stationFaultRecordDto;
    }
}

